var a, t = require("../../../mymap/qqmap-wx-jssdk.min.js"), e = getApp();

Page({
    data: {
        suggestion: [],
        address_latitude: "",
        address_longitude: "",
        markers: [],
        address_detail: "",
        suggestionshow: !1,
        mksData: [],
        mapmessageData: {
            title: "",
            message: "",
            latitude: "",
            longitude: "",
            city: ""
        },
        scaleData: 16,
        tag: ""
    },
    onLoad: function(s) {
        var i = this;
        s.id && this.setData({
            id: s.id
        }), s.store_id && this.setData({
            store_id: s.store_id,
            form: JSON.parse(s.form),
            create: s.create
        }), e.globalData.qqmapkey ? (a = new t({
            key: e.globalData.qqmapkey
        }), s.tag && this.setData({
            tag: s.tag
        }), this.poscurrent()) : e._getTemplate().then(function(o) {
            a = new t({
                key: e.globalData.qqmapkey
            }), s.tag && i.setData({
                tag: s.tag
            }), i.poscurrent();
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    searchconfirmbtn: function(a) {
        var t = a.detail.value;
        this.addressinp(t);
    },
    addressinpbtn: function(a) {
        var t = a.detail.value;
        this.addressinp(t);
    },
    addressinp: function(t) {
        var s = this;
        t ? a.getSuggestion({
            keyword: t,
            sig: e.globalData.qqmapsig,
            success: function(a) {
                for (var t = [], e = 0; e < a.data.length; e++) t.push({
                    title: a.data[e].title,
                    id: a.data[e].id,
                    addr: a.data[e].address,
                    city: a.data[e].city,
                    district: a.data[e].district,
                    latitude: a.data[e].location.lat,
                    longitude: a.data[e].location.lng
                });
                s.setData({
                    suggestion: t,
                    suggestionshow: !0
                });
            },
            fail: function(a) {
                console.error(a);
            },
            complete: function(a) {}
        }) : this.setData({
            suggestionshow: !1
        });
    },
    backfill: function(a) {
        for (var t = a.currentTarget.id, e = 0; e < this.data.suggestion.length; e++) e == t && (this.setData({
            address_detail: this.data.suggestion[e].title,
            address_latitude: this.data.suggestion[e].latitude,
            address_longitude: this.data.suggestion[e].longitude,
            "mapmessageData.title": this.data.suggestion[e].title,
            "mapmessageData.message": this.data.suggestion[e].addr,
            "mapmessageData.latitude": this.data.suggestion[e].latitude,
            "mapmessageData.longitude": this.data.suggestion[e].longitude,
            "mapmessageData.city": this.data.suggestion[e].city,
            suggestionshow: !1
        }), this.nearby_search());
    },
    nearby_search: function() {
        var t = this;
        a.search({
            keyword: this.data.address_detail,
            location: {
                latitude: this.data.address_latitude,
                longitude: this.data.address_longitude
            },
            sig: e.globalData.qqmapsig,
            success: function(a) {
                console.log("ccccc", a);
                for (var e = [], s = [], i = 0; i < a.data.length; i++) e.push({
                    title: a.data[i].title,
                    id: a.data[i].id,
                    latitude: a.data[i].location.lat,
                    longitude: a.data[i].location.lng,
                    iconPath: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
                    width: 20,
                    height: 25,
                    callout: {
                        padding: "10rpx",
                        borderRadius: "5rpx",
                        content: a.data[i].title
                    }
                }), s.push({
                    address: a.data[i].address,
                    title: a.data[i].title,
                    latitude: a.data[i].location.lat,
                    longitude: a.data[i].location.lng,
                    id: a.data[i].id,
                    city: a.data[i].ad_info.city ? a.data[i].ad_info.city : a.res.data[i].ad_info.province
                });
                t.setData({
                    markers: e,
                    mksData: s
                });
            },
            fail: function(a) {
                console.log(a);
            },
            complete: function(a) {
                console.log(a);
            }
        });
    },
    getmarkertap: function(a) {
        for (var t = a.markerId, e = 0; e < this.data.mksData.length; e++) {
            t == this.data.mksData[e].id && this.setData({
                "mapmessageData.title": this.data.mksData[e].title,
                "mapmessageData.message": this.data.mksData[e].address,
                "mapmessageData.latitude": this.data.mksData[e].latitude,
                "mapmessageData.longitude": this.data.mksData[e].longitude,
                "mapmessageData.city": this.data.mksData[e].city
            });
        }
    },
    addbtn: function() {
        var a = wx.createMapContext("myMap"), t = this;
        a.getScale({
            success: function(a) {
                var e = a.scale;
                t.setData({
                    scaleData: e + 1
                });
            }
        });
    },
    minusbtn: function() {
        var a = wx.createMapContext("myMap"), t = this;
        a.getScale({
            success: function(a) {
                var e = a.scale;
                t.setData({
                    scaleData: e - 1
                });
            }
        });
    },
    addressplay: function() {
        this.poscurrent();
    },
    poscurrent: function() {
        var a = this;
        wx.getSetting({
            success: function(t) {
                t.authSetting["scope.userLocation"] ? a.getlocaldata() : wx.authorize({
                    scope: "scope.userLocation",
                    success: function() {},
                    fail: function(a) {},
                    complete: function() {
                        a.getlocaldata();
                    }
                });
            }
        });
    },
    getlocaldata: function() {
        var a = this;
        wx.getLocation({
            type: "gcj02",
            success: function(t) {
                var e = t.latitude, s = t.longitude;
                a._setaddressData({
                    lat: e,
                    lng: s
                });
            },
            fail: function(t) {
                "getLocation:fail auth deny" === t.errMsg && (console.log("当初用户拒绝，再次发起授权"), wx.openSetting({
                    success: function(t) {
                        t.authSetting["scope.userLocation"] ? (wx.showToast({
                            icon: "none",
                            title: "获取权限成功",
                            duration: 1500
                        }), a.getlocaldata()) : wx.showToast({
                            icon: "none",
                            title: "获取权限失败,定位需要用户授权",
                            duration: 1500
                        });
                    }
                }));
            }
        });
    },
    _setaddressData: function(a) {
        var t = this;
        e.getReverseGeocoder(a.lat, a.lng).then(function(a) {
            t.setData({
                address_detail: "",
                address_latitude: a.location.lat,
                address_longitude: a.location.lng,
                "mapmessageData.title": a.formatted_addresses.recommend,
                "mapmessageData.address_component": a.formatted_addresses,
                "mapmessageData.message": a.address,
                "mapmessageData.latitude": a.location.lat,
                "mapmessageData.longitude": a.location.lng,
                "mapmessageData.city": a.address_component.city ? a.address_component.city : a.address_component.province
            });
        }).catch(function(a) {
            console.log(a);
        });
    },
    buttonbtn: function() {
        var a, t, e = {
            lat: this.data.address_latitude,
            lng: this.data.address_longitude
        };
        if (console.log(this.data.mapmessageData, e, "point"), wx.setStorage({
            key: "address-point",
            data: {
                point: e,
                city: this.data.mapmessageData.city,
                address_component: this.data.mapmessageData.address_component
            }
        }), "hotel" == this.data.tag) wx.reLaunch({
            url: "/packageC/hotel/HotelIndex/HotelIndex?addressData=" + JSON.stringify(this.data.mapmessageData)
        }); else if ("home" == this.data.tag) wx.reLaunch({
            url: "/packageG/index/index?addressData=" + JSON.stringify(this.data.mapmessageData)
        }); else if ("easyRefuel" == this.data.tag) wx.reLaunch({
            url: "/packageD/easyRefuel/easyRefuel"
        }); else if ("storeConsume" == this.data.tag) wx.reLaunch({
            url: "/packageH/o2o/storeConsumeIndex/storeConsumeIndex?id=" + this.data.id
        }); else if ("cake" == this.data.tag) wx.reLaunch({
            url: "/packageF/sachertorte/sachertorteIndex/sachertorteIndex?city=" + this.data.mapmessageData.city
        }); else if ("appointment" == this.data.tag) {
            console.log(this.data.mapmessageData);
            var s = {
                lat: this.data.mapmessageData.latitude,
                lng: this.data.mapmessageData.longitude
            }, i = {
                address: this.data.mapmessageData.message,
                city: this.data.mapmessageData.city,
                title: this.data.mapmessageData.title,
                point: s
            };
            (a = (t = getCurrentPages())[t.length - 3]).setData({
                pos: i
            }), wx.navigateBack({
                delta: 2
            });
        } else if ("diy" == this.data.tag) {
            var o = wx.getStorageSync("diyurl");
            wx.reLaunch({
                url: o
            });
        } else if ("activityHome" == this.data.tag) wx.reLaunch({
            url: "/packageC/Myshaky/activityHome/activityHome?city=" + this.data.mapmessageData.city
        }); else if ("store" == this.data.tag) wx.navigateTo({
            url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=" + this.data.tag + "&store_id=" + this.data.store_id + "&form=" + JSON.stringify(this.data.form) + "&create=" + this.data.create + "&is_point=" + JSON.stringify(e)
        }); else if ("storeApply" == this.data.tag || "selfCarryApply" == this.data.tag) try {
            wx.setStorageSync("applyaddresspoint", {
                address: this.data.mapmessageData.message,
                point: e,
                city: this.data.mapmessageData.city
            }), wx.navigateBack({
                delta: 1,
                fail: function() {}
            });
        } catch (a) {
            console.log(a);
        } else if ("communityStore" == this.data.tag || "communityStoreDetails" == this.data.tag) {
            var d = {
                lat: this.data.mapmessageData.latitude,
                lng: this.data.mapmessageData.longitude
            }, n = {
                address: this.data.mapmessageData.message,
                city: this.data.mapmessageData.city,
                title: this.data.mapmessageData.title,
                point: d
            };
            (a = (t = getCurrentPages())[t.length - 3]).setData(n), a._getData(), wx.navigateBack({
                delta: 2
            });
        } else if (-1 !== this.data.tag.indexOf("project_verification")) switch (wx.setStorage({
            key: "address-point",
            data: {
                point: {
                    lat: this.data.mapmessageData.latitude,
                    lng: this.data.mapmessageData.longitude
                },
                city: this.data.mapmessageData.message,
                address_component: this.data.mapmessageData.address_component
            }
        }), this.data.tag) {
          case "project_verification/MyProject":
            wx.reLaunch({
                url: "/packageH/project_verification/MyProject/MyProject?location_change=1"
            });
            break;

          case "project_verification/VerificationProjectList":
            wx.reLaunch({
                url: "/packageH/project_verification/VerificationProjectList/VerificationProjectList?location_change=1"
            });
        } else "suppligoodsOrde" == this.data.tag ? wx.navigateBack({
            delta: 1
        }) : "tagCloud" == this.data.tag ? wx.navigateBack({
            delta: 2
        }) : wx.reLaunch({
            url: "/packageC/o2o/o2oHome/o2oHome?addressData=" + JSON.stringify(this.data.mapmessageData)
        });
    }
});